// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import styles from './BomFaultGrid.css'
import LinkToDeviceOverView from './LinkToDeviceOverView'
import { TableResize } from 'ag-grid-addons'
import moment from 'moment'
import { connect } from 'react-redux'
// import rowData from './rowData'

type Props = {
  history: Object,
  rowData: Array<Object>,
  deviceId: number
}
type States = {
  columnDefs: Array<Object>
}
export class BomGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  api: Object
  onGridReady: () => void
  TableResize: () => void
  goToDeviceOverView: () => void
  constructor (props: Props) {
    super(props)
    const component = this
    this.state = {
      columnDefs: [
        { headerName: '事件名称',
          field: 'eventType',
          width: 120,
          cellRendererParams: {
            goToDeviceOverView: component.goToDeviceOverView.bind(this)
          },
          cellRendererFramework: LinkToDeviceOverView,
          cellStyle: {
            'padding': '8px'
          }
        },
        { headerName: '开始时间',
          field: 'startTime',
          width: 180,
          // cellRenderer: (params) => {
          //   return moment(params.data.startTime).format('YYYY-MM-DD HH:mm:ss')
          // },
          cellStyle: {
            'padding': '8px',
            'text-align':'center'
          }
        },
        { headerName: '结束时间',
          field: 'endTime',
          width: 180,
          // cellRenderer: (params) => {
          //   return moment(params.data.endTime).format('YYYY-MM-DD HH:mm:ss')
          // },
          cellStyle: {
            'padding': '8px',
            'text-align':'center'
          }
        },
        { headerName: '负责人',
          field: 'owner',
          width: 150,
          cellStyle: {
            'padding': '8px',
            'text-align':'center'
          }
        },
        { headerName: '处理状态',
          field: 'status',
          width: 150,
          cellStyle: {
            'padding': '8px',
            'text-align':'center'
          }
        },
        { headerName: '事件描述',
          field: 'detail',
          width: 150,
          cellStyle: {
            'padding': '8px',
            'text-align':'center'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      suppressPaginationPanel: true,
      enableServerSideSorting: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
    this.goToDeviceOverView = this.goToDeviceOverView.bind(this)
  }
  goToDeviceOverView (data: Object) {
    const { deviceId, history } = this.props
    const location = {
      pathname:`/device-file/${deviceId}/condition`,
      state: {
        defaultActiveKey: 'dynamicTab'
      }
    }
    history.push(location)
  }
  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }

  render () {
    const { rowData } = this.props
    const { columnDefs } = this.state
    return (
      <div className={styles['grid']}>
        <div className={styles['content'] + ' ag-fresh'}>
          <AgGridReact
            gridOptions={this.gridOptions}
            rowHeight='34'
            headerHeight='28'
            columnDefs={columnDefs}
            rowData={rowData}
            onGridReady={this.onGridReady}
          />
        </div>
      </div>
    )
  }
}
const mapStatesToProps = (state) => {
  return {
    deviceId: state.deviceCard.deviceData.id
  }
}
export default connect(mapStatesToProps, {})(BomGrid)
